﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using VatTuDAO;
using VatTuDTO1;
using System.Data;

namespace VatTuDAO1
{
    public class TinhTrangDAO : BaseDataProvider
    {
        MySqlCommand _cmd;

        //Lay mot loai nguoi dung 
        public TinhTrangDTO SelectTinhTrang(int maTinhTrang)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from TinhTrang where MaTinhTrang=@MaTinhTrang";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrang", maTinhTrang));

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            DataRow row = dt.Rows[0];
            if (row == null)
                return null;
            TinhTrangDTO dto = new TinhTrangDTO();
            dto.MaTinhTrang= int.Parse(row["MaTinhTrang"].ToString());
            dto.TenTinhTrang = row["TenTinhTrang"].ToString();
       
            return dto;
        }

        //  Lay danh sach loai nguoi dung 
        public List<TinhTrangDTO> SelectTinhTrang()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from TinhTrang";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            List<TinhTrangDTO> ds = new List<TinhTrangDTO>();
            TinhTrangDTO dto;

            try
            {

                foreach (DataRow row in dt.Rows)
                {
                    dto = new TinhTrangDTO();
                    dto.MaTinhTrang = int.Parse(row["MaTinhTrang"].ToString());
                    dto.TenTinhTrang = row["TenTinhTrang"].ToString();
                   


                    ds.Add(dto);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ds;
        }

      

       

   


       

    }
}
